//
//  TravelViewController.m
//  W.E.S.T
//
//  Created by xalo on 16/7/13.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "TravelViewController.h"

@interface TravelViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UIScrollView *scrollView;

@property (nonatomic,strong)UIView *whileView;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *scrollArray;
@property (nonatomic,strong)NSMutableArray *tableViewArray;
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *collectionArray;
@property (nonatomic,strong)UILabel *collectionViewLabel;
@property (nonatomic,strong)UILabel *tableViewLabel;
@property (nonatomic,assign)NSInteger start;
@property (nonatomic,assign)NSInteger limit;
@property (nonatomic,strong)UIButton *button;



@end

@implementation TravelViewController


- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
        layout.itemSize=CGSizeMake(150, 100);
        layout.sectionInset=UIEdgeInsetsMake(0, 10, 0, 10);
        _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, kHeight/3+30, kWidth, kHeight/3) collectionViewLayout:layout];
        _collectionView.dataSource=self;
        _collectionView.delegate=self;
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.bounces=NO;
        _collectionView.bouncesZoom=NO;
        _collectionView.showsVerticalScrollIndicator=NO;
    }
    return _collectionView;
}
- (UILabel *)tableViewLabel{
    if (!_tableViewLabel) {
        _tableViewLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, (2*kHeight)/3+80, kWidth, 20)];
        _tableViewLabel.text=@" 精华游记";
        _tableViewLabel.backgroundColor=[UIColor whiteColor];
        _tableViewLabel.textColor=[UIColor blackColor];
        _tableViewLabel.textAlignment=NSTextAlignmentLeft;
    }
    return _tableViewLabel;
}
- (NSMutableArray *)collectionArray{
    if (!_collectionArray) {
        _collectionArray=[NSMutableArray array];
    }
    return _collectionArray;
}
- (UILabel *)collectionViewLabel{
    if (!_collectionViewLabel) {
        _collectionViewLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, kHeight/3+20, kWidth/3, 20)];
        _collectionViewLabel.text=@" 折扣推荐";
        _collectionViewLabel.backgroundColor=[UIColor cyanColor];
        _collectionViewLabel.textColor=[UIColor blackColor];
        _collectionViewLabel.textAlignment=NSTextAlignmentLeft;
    }
    return _collectionViewLabel;
}
//- (UIButton *)button{
//    if (!_button) {
//        _button=[[UIButton alloc]initWithFrame:CGRectMake((2*kWidth)/3, kHeight/3+20, kWidth/3, 20)];
//        _button.backgroundColor=[UIColor cyanColor];
//        [_button setTitle:@"旅游社区>" forState:UIControlStateNormal];
//        _button.tintColor=[UIColor blackColor];
//    }
//    return _button;
//}
-(UIView *)whileView{
    if (!_whileView) {
        _whileView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth,(2*kHeight)/3 + 120)];
        
    }
    return _whileView;
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
    }
    return _tableView;
}
- (NSMutableArray *)scrollArray{
    if (!_scrollArray) {
        _scrollArray=[NSMutableArray array];
    }
    return _scrollArray;
}


- (NSMutableArray *)tableViewArray{
    if (!_tableViewArray) {
        _tableViewArray=[NSMutableArray array];
    }
    return _tableViewArray;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
   // self.automaticallyAdjustsScrollViewInsets=NO;
    //边缘修饰
    self.edgesForExtendedLayout=UIRectEdgeNone;
    [self.view addSubview:self.tableView];
    //添加旅游社区
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"旅游社区" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction)];
    //XIB注册collectionViewCell
    [self.collectionView registerNib:[UINib nibWithNibName:@"TravelCell" bundle:nil] forCellWithReuseIdentifier:@"TravelModel_cell"];
    
    //XIB注册tabelViewCell
    [self.tableView registerNib:[UINib nibWithNibName:@"TravelBestCell" bundle:nil] forCellReuseIdentifier:@"TravelBestModel_cell"];
    //请求数据
    [RequestManager fetchDataWithUrl:kTravelUrl parameter:@{} method:@"GET" compeleted:^(NSData *data) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
       // NSLog(@"%@",dic);
        //NSLog(@"%@",dic[@"data"][@"discount"]);
        for (NSDictionary *scrollDic in dic[@"data"][@"slide"]) {
           
            [self.scrollArray addObject:scrollDic[@"photo"]];
            // NSLog(@"%@",self.scrollArray);
        }
        for (NSDictionary *collectionDic in dic[@"data"][@"discount"]) {
            
            TravelModel *model=[[TravelModel alloc]init];
            [model setValuesForKeysWithDictionary:collectionDic];
            [self.collectionArray addObject:model];
        }
 
       
    } updateUI:^{
        
        WHZScrollView *scrollView=[[WHZScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3) imageArray:self.scrollArray];
        
        [self.whileView addSubview:scrollView];
        [self.whileView addSubview:self.collectionViewLabel];
        [self.whileView addSubview:self.button];
        [self.whileView addSubview:self.tableViewLabel];
        [self.whileView addSubview:self.collectionView];
        
        self.tableView.tableHeaderView=self.whileView;
        
        [self.tableView reloadData];
    }];
    //tableView上展示的数据请求
    [RequestManager fetchDataWithUrl:kTravelTripUrl parameter:@{} method:@"GET" compeleted:^(NSData *data) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
       // NSLog(@"%@",dic[@"data"]);
        for (NSDictionary *tableDic in dic[@"data"]) {
            TravelModel *model=[[TravelModel alloc]init];
            [model setValuesForKeysWithDictionary:tableDic];
            [self.tableViewArray addObject:model];
        }
    } updateUI:^{
        
        [self.tableView reloadData];
    }];
    
    //上拉加载
    self.tableView.mj_footer=[MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.start+=5;
        [self loadTableViewDataSourceWithStart:self.start limit:5];
    }];
  
}
//旅游社区点击事件
- (void)itemAction{
    TravelCommunityViewController *travelCommunityVC=[[TravelCommunityViewController alloc]init];
    [self.navigationController pushViewController:travelCommunityVC animated:YES];
}
//网络请求
- (void)loadTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fetchDataWithUrl:kTravelTripLoadUrl parameter:@{} method:@"GET" compeleted:^(NSData *data) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        //NSLog(@"%@",dic[@"data"]);
        for (NSDictionary *tableDic in dic[@"data"]) {
            TravelModel *model=[[TravelModel alloc]init];
            [model setValuesForKeysWithDictionary:tableDic];
            [self.tableViewArray addObject:model];
        }
     
        
    } updateUI:^{
        //上拉加载结束
        [self.tableView.mj_footer endRefreshing];
        
        [self.tableView reloadData];
        
  }];
}
#pragma mark-----------UICollectionView的协议----------------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.collectionArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TravelCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"TravelModel_cell" forIndexPath:indexPath];
    [cell setCellWithModel:self.collectionArray[indexPath.row]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TravelTripViewController *travelTripVC=[[TravelTripViewController alloc]init];
    travelTripVC.discountID=[self.collectionArray[indexPath.row] ids];
    [self.navigationController pushViewController:travelTripVC animated:YES];
}
#pragma mark------------UITableView的协议-----------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tableViewArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TravelBestCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TravelBestModel_cell" forIndexPath:indexPath];
    [cell setCellWithModel:self.tableViewArray[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TravelTripViewController *travelTripVC=[[TravelTripViewController alloc]init];
    travelTripVC.typeid=[self.tableViewArray[indexPath.row] ID];
    
    [self.navigationController pushViewController:travelTripVC animated:YES];
}



















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
